package com.lianniu.mgr.baseService.post.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import com.lianniu.mgr.base.BaseResult;
import com.lianniu.mgr.baseService.area.model.SystemAreaVo;
import com.lianniu.mgr.baseService.menu.model.MenuVo;
import com.lianniu.mgr.baseService.post.dao.IAssignAuthorityMapperDao;
import com.lianniu.mgr.baseService.post.model.Post2Menu2OpVo;
import com.lianniu.mgr.baseService.post.model.Post2SysAreaVo;
import com.lianniu.mgr.baseService.post.model.Sys2Menu2OpVo;
import com.lianniu.mgr.baseService.post.service.IAssignAuthorityService;
import com.lianniu.mgr.constants.ErrorCode;
import com.lianniu.mgr.util.StringUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;

@Service
public class AssignAuthorityServiceImpl implements IAssignAuthorityService {

  private final static Logger logger = Logger.getLogger(AssignAuthorityServiceImpl.class);

  @Resource
  private IAssignAuthorityMapperDao assignAuthorityMapperDao;

  /**
   * @Description:查询所有菜单
   * @return BaseResult(data=List<MenuVo>)

   * @Date:2016年3月29日下午8:34:29
   * @Version 1.0.0
   */
  @Override
  public BaseResult getAllMenu() {
	logger.info("AssignAuthorityServiceImpl getAllMenu begin.");
    logger.info("getAllMenu begin.");
    List<MenuVo> list = assignAuthorityMapperDao.getAllMenu();
    logger.info("getAllMenu end.");
    logger.info("AssignAuthorityServiceImpl getAllMenu end.");
    return BaseResult.success(list);
  }

  /**
   * @Description:查询所有区域
   * @return BaseResult(data=List<SystemAreaVo>)

   * @Date:2016年3月29日下午8:37:55
   * @Version 1.1.0
   */
  @Override
  public BaseResult getAllArea() {
	logger.info("AssignAuthorityServiceImpl getAllArea begin.");
    logger.info("getAllArea begin.");
    List<SystemAreaVo> list = assignAuthorityMapperDao.getAllArea();
    logger.info("getAllArea end.");
    logger.info("AssignAuthorityServiceImpl getAllArea end.");
    return BaseResult.success(list);
  }

  /**
   * @Description:保存分配的区域
   * @param areaIdArr
   * @param postId
   * @return BaseResult

   * @Date:2016年4月1日下午1:18:37
   * @Version 1.1.0
   */
  @Override
  @Transactional
  public BaseResult addPrecinct(String[] areaIdArr, String postId) {
    logger.info("AssignAuthorityServiceImpl addPrecinct begin.");
    logger.info("areaIdArr:" + JSON.toJSONString(areaIdArr));
    logger.info("postId:" + postId);

    logger.info("input data check begin.");
    if(areaIdArr!=null&&areaIdArr.length>0){
      for (String areaId : areaIdArr) {
        if (StringUtil.isEmpty(areaId)) {
          return BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
        }
      }
    }
    
    if (StringUtil.isEmpty(postId)) {
      return BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
    }
    logger.info("input data check begin.");

    logger.info("delPrecinct begin.");
    assignAuthorityMapperDao.delPrecinct(postId);
    logger.info("delPrecinct end.");

    List<Post2SysAreaVo> list=new ArrayList<Post2SysAreaVo>();
    //如果没有节点被勾选，则不插入数据
    if(areaIdArr!=null&&areaIdArr.length>0){
      List<String> areaIdLi = new ArrayList<String>();
      for (String areaId : areaIdArr) {
          areaIdLi.add(areaId);
      }
      
      logger.info("getParentByAreaId begin.");
      List<String> allAreaIdLi=assignAuthorityMapperDao.getParentByAreaId(areaIdLi);
      logger.info("getParentByAreaId end.");
      
      for(String str:allAreaIdLi){
          Post2SysAreaVo vo=new Post2SysAreaVo();
          vo.setAreaId(str);
          vo.setPostId(postId);
          vo.setId(StringUtil.getUUID());
          list.add(vo);
      }
      
      logger.info("addPrecinct begin.");
      assignAuthorityMapperDao.addPrecinct(list);
      logger.info("addPrecinct end.");
    }
    
    logger.info("AssignAuthorityServiceImpl addPrecinct end.");
    return BaseResult.success(null);
  }

  /**
   * @Description:保存分配的菜单 
   * 根据post_id删除lianniu_post2menu和lianniu_post2menu2Op中的记录，在向这两张表中插入所选内容，其中lianniu_post2menu2Op不插op_id
   * @param menuIdArr
   * @param postId
   * @return BaseResult

   * @Date:2016年4月1日下午1:59:50
   * @Version 1.0.0
   */
  @Override
  @Transactional
  public BaseResult addMenu(String[] menuIdArr, String postId) {
    logger.info("AssignAuthorityServiceImpl addMenu begin.");
    logger.info("menuIdArr:" + menuIdArr);
    logger.info("postId:" + postId);

    logger.info("input data check begin.");
    if(menuIdArr!=null&&menuIdArr.length>0){
      for (String menuId : menuIdArr) {
        if (StringUtil.isEmpty(menuId)) {
          return BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
        }
      }
    }
    
    if (StringUtil.isEmpty(postId)) {
      return BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
    }
    logger.info("input data check begin.");

    // 删除lianniu_post2menu和lianniu_post2menu2Op中的记录
    logger.info("delMenu begin.");
    assignAuthorityMapperDao.delMenu(postId);
    logger.info("delMenu end.");
    
    logger.info("delOperation begin.");
    assignAuthorityMapperDao.delOperation(postId);
    logger.info("delOperation end.");

    List<Post2Menu2OpVo> list = new ArrayList<Post2Menu2OpVo>();
    //有节点被勾选则插入数据
    if(menuIdArr!=null&&menuIdArr.length>0){
      for (String menuId : menuIdArr) {
        Post2Menu2OpVo post2Menu2OpVo = new Post2Menu2OpVo();
        post2Menu2OpVo.setId(StringUtil.getUUID());
        post2Menu2OpVo.setPostId(postId);
        post2Menu2OpVo.setMenuId(menuId);
        list.add(post2Menu2OpVo);
      }
      // 向lianniu_post2menu和lianniu_post2menu2Op插值
      logger.info("addMenu begin.");
      assignAuthorityMapperDao.addMenu(list);
      logger.info("addMenu end.");
      
      logger.info("addMenu2Po begin.");
      assignAuthorityMapperDao.addMenu2Po(list);
      logger.info("addMenu2Po end.");
    }
    
    logger.info("AssignAuthorityServiceImpl addMenu end.");
    return BaseResult.success(null);
  }

  /**
   * @Description:查询勾选的各级菜单id
   * @param postId
   * @return BaseResult

   * @Date:2016年4月5日下午1:37:52
   * @Version 1.1.0
   */
  @Override
  public BaseResult getCheckedMenuByPostId(String postId) {
    logger.info("AssignAuthorityServiceImpl getCheckedMenuByPostId begin.");
    logger.info("postId:" + postId);

    logger.info("input data check begin.");
    if (StringUtil.isEmpty(postId)) {
      return BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
    }
    logger.info("input data check end.");
    
    logger.info("getCheckedMenuByPostId begin.");
    List<MenuVo> list = assignAuthorityMapperDao.getCheckedMenuByPostId(postId);
    logger.info("getCheckedMenuByPostId end.");
    
    logger.info("AssignAuthorityServiceImpl getCheckedMenuByPostId end.");
    return BaseResult.success(list);
  }

  /**
   * @Description:分配操作功能保存
   * @param opArr
   * @param postId
   * @return BaseResult

   * @Date:2016年4月5日下午1:38:32
   * @Version 1.1.0
   */
  @Override
  @Transactional
  public BaseResult addOperation(String[] opArr, String postId) {
    logger.info("AssignAuthorityServiceImpl addOperation begin.");
    logger.info("opArr:"+JSON.toJSONString(opArr));
    logger.info("postId:"+postId);
    
    logger.info("input data check begin.");
    if (StringUtil.isEmpty(postId)) {
      return BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
    }
    if(!(opArr!=null&&opArr.length>0)){
      return BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
    }
    logger.info("input data check end.");

    //opArr数组中的每一个元素为，三级menuId和每一个菜单对应的opId，以逗号分隔
    List<Post2Menu2OpVo> list = new ArrayList<Post2Menu2OpVo>();
  	for (String str : opArr) {
  	  String[] arr=str.split(",");
  	  for(int i=1;i<arr.length;++i){
  	    Post2Menu2OpVo vo = new Post2Menu2OpVo();
  	    vo.setMenuId(arr[0]);
  	    vo.setOpId(arr[i]);
  	    vo.setPostId(postId);
          vo.setId(StringUtil.getUUID());
          list.add(vo);
  	  }
	}
    
    logger.info("delOperation begin.");
    assignAuthorityMapperDao.delOperation(postId);
    logger.info("delOperation begin.");
    
    if(!StringUtil.isEmpty(list)){
    	logger.info("addOperation begin.");
        assignAuthorityMapperDao.addOperation(list);
        logger.info("addOperation end.");
    }
    
    logger.info("AssignAuthorityServiceImpl addOperation end.");
    return BaseResult.success(null);
  }

  /**
   * @Description:根据postId获得系统菜单
   * @param postId
   * @return BaseResult
   * @Date:2016年4月8日上午11:10:58
   * @Version 1.0.0
   */
  @Override
  public BaseResult getSysMenuByPostId(String postId) {
    logger.info("AssignAuthorityServiceImpl getSysMenuByPostId begin.");
    logger.info("postId:"+postId);

    logger.info("input data check begin.");
    if (StringUtil.isEmpty(postId)) {
      return BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
    }
    logger.info("input data check end.");

    logger.info("getSysMenuByPostId begin.");
    List<Sys2Menu2OpVo> li=assignAuthorityMapperDao.getSysMenuByPostId(postId);
    logger.info("getSysMenuByPostId end.");

    logger.info("AssignAuthorityServiceImpl getSysMenuByPostId end.");
    return BaseResult.success(li);
  }

  /**
   * @Description:根据菜单id查询功能名称
   * @param menuId
   * @return BaseResult

   * @Date:2016年4月10日下午6:31:24
   * @Version 1.0.0
   */
  @Override
  public BaseResult getOpByMenuId(String menuId) {
    logger.info("AssignAuthorityServiceImpl getOpByMenuId begin.");
    logger.info("menuId:"+menuId);
    
    logger.info("input data check begin.");
    if (StringUtil.isEmpty(menuId)) {
      return BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
    }
    logger.info("input data check end.");
    
    logger.info("getOpByMenuId begin.");
    List<Sys2Menu2OpVo> li=assignAuthorityMapperDao.getOpByMenuId(menuId);
    logger.info("getOpByMenuId end.");
    
    logger.info("AssignAuthorityServiceImpl getOpByMenuId end.");
    return BaseResult.success(li);
  }

  /**
   * @Description:根据岗位id查询该岗位下已被勾选的功能操作及对应菜单
   * @param postId
   * @return BaseResult
   * @Date:2016年5月12日上午11:01:35
   * @Version 1.1.0
   */
  @Override
  public BaseResult getCheckedOpByPostId(String postId) {
    logger.info("AssignAuthorityServiceImpl getCheckedOpByPostId begin.");
    logger.info("postId:"+postId);

    logger.info("input data check begin.");
    if(StringUtil.isEmpty(postId)){
      return BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
    }
    logger.info("input data check end.");

    logger.info("getCheckedOpByPostId begin.");
    List<Sys2Menu2OpVo> li=assignAuthorityMapperDao.getCheckedOpByPostId(postId);
    logger.info("getCheckedOpByPostId end.");

    logger.info("AssignAuthorityServiceImpl getCheckedOpByPostId end.");
    return BaseResult.success(li);
  }

}
